package com.hq.admin.service;

import com.hq.admin.dao.MenuDao;
import com.hq.admin.dao.OperatorDAO;
import com.hq.admin.domain.Menu;
import com.hq.admin.domain.Operator;
import com.hq.base.dao.BaseDAO;
import com.hq.base.domain.BaseObject;
import com.hq.base.service.BaseService;
import com.hq.common.domain.SearchObject;
import com.hq.common.domain.SearchValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OperatorService extends BaseService<Operator> {
    @Autowired
    private OperatorDAO operatorDAO;
    @Autowired
    private MenuDao menuDao;

    private OperatorDAO getUser() {
        return operatorDAO;
    }

    public void insert(Operator operator) {
        operatorDAO.saveOrUpdate(operator);
    }


    @Override
    public BaseDAO<Operator> getDao() {
        return operatorDAO;
    }

    public void update(Operator operator) {
        operatorDAO.update(operator);
    }



    public List<Operator> query(Operator operator) {
        return operatorDAO.get(operator);
    }


    public List<Operator> queryByName(String name,String password){
        return operatorDAO.queryByName(name, password);
    }

    public Operator login(String name,String password){
        List<Operator> operatorList = operatorDAO.queryByName(name, password);
        if(operatorList ==null|| operatorList.size()!=1){
            return null;
        }
        Operator operator=operatorList.get(0);
        Menu menuQuery=new Menu();
        menuQuery.setParent_id(0L);
        SearchObject searchObject=new SearchObject();
        searchObject.addCondition("parent_id",0L, SearchValue.Pattern.EQUAL);
        List<Menu> menu_1st=menuDao.get(searchObject);
        operator.setMenu_1st(menu_1st);
        searchObject=new SearchObject();
        searchObject.addCondition("parent_id",0L, SearchValue.Pattern.NOTEQUAL);
        List<Menu> menu_2nd=menuDao.get(searchObject);
        operator.setMenu_2nd(menu_2nd);
        return operator;
    }
}
